﻿// CWellTimeSpanSetDlg.cpp: 实现文件
//

#include "pch.h"
#include "RTOC-Data-Interface.h"
#include "CWellTimeSpanSetDlg.h"
#include "afxdialogex.h"


// CWellTimeSpanSetDlg 对话框

IMPLEMENT_DYNAMIC(CWellTimeSpanSetDlg, CDialogEx)

CWellTimeSpanSetDlg::CWellTimeSpanSetDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_WELLTIMESPANDLG, pParent)
{

}

CWellTimeSpanSetDlg::~CWellTimeSpanSetDlg()
{
}

void CWellTimeSpanSetDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST1, m_ctlList);
}


BEGIN_MESSAGE_MAP(CWellTimeSpanSetDlg, CDialogEx)
	ON_NOTIFY(NM_CLICK, IDC_LIST1, &CWellTimeSpanSetDlg::OnNMClickList1)
	ON_BN_CLICKED(IDC_APPLY, &CWellTimeSpanSetDlg::OnBnClickedApply)
END_MESSAGE_MAP()


// CWellTimeSpanSetDlg 消息处理程序


BOOL CWellTimeSpanSetDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
	m_ctlList.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
	m_ctlList.InsertColumn(0, _T("序号"), LVCFMT_LEFT, 40);
	m_ctlList.InsertColumn(1, _T("井名"), LVCFMT_LEFT, 120);
	m_ctlList.InsertColumn(2, _T("时间偏差"), LVCFMT_LEFT, 80);

	FillList();
	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}

void CWellTimeSpanSetDlg::OnNMClickList1(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码
	m_nCurSelRow = m_ctlList.GetSelectionMark();
	CString strTemp;
	if (m_nCurSelRow >= 0)
	{
		m_strCurWN = m_ctlList.GetItemText(m_nCurSelRow, 1);
		m_strCurWN.Trim();

		GetDlgItem(IDC_WN)->SetWindowText(m_strCurWN);

		strTemp = m_ctlList.GetItemText(m_nCurSelRow, 2);
		GetDlgItem(IDC_TIMESPAN)->SetWindowText(strTemp);
	}

	*pResult = 0;
}

void CWellTimeSpanSetDlg::FillList()
{
	m_ctlList.DeleteAllItems();
	CString strTemp;
	int nRows = gl.tlm.m_aWells.GetCount();
	for (int i = 0; i < nRows; i++)
	{
		SINGLE_WELL sw = gl.tlm.m_aWells.GetAt(i);
		strTemp.Format(_T("%d"), i + 1);
		m_ctlList.InsertItem(i, strTemp, 0);
		m_ctlList.SetItemText(i, 1, sw.strWN);
		strTemp.Format(_T("%d"), sw.nTimeSpan);
		m_ctlList.SetItemText(i, 2, strTemp);
	}
}


void CWellTimeSpanSetDlg::OnBnClickedApply()
{
	// TODO: 在此添加控件通知处理程序代码
	CString strTemp;
	if (m_nCurSelRow >= 0)
	{
		SINGLE_WELL sw=gl.tlm.m_aWells.GetAt(m_nCurSelRow);
		GetDlgItem(IDC_TIMESPAN)->GetWindowTextW(strTemp);
		int nSpan = _wtol(strTemp);
		if (nSpan < 30)
		{
			AfxMessageBox(_T("时间偏差建议大于30秒，才有可能获取到数据！"), MB_ICONINFORMATION);
			return;
		}
		sw.nTimeSpan = nSpan;
		::EnterCriticalSection(&m_cWS);
		gl.tlm.m_aWells.SetAt(m_nCurSelRow,sw);
		::LeaveCriticalSection(&m_cWS);
		FillList();
	}
}
